/**
 * <pre>
 * Copyright Notice:
 *    Copyright (c) 2005-2009 China Financial Certification Authority(CFCA)
 *    A-1 You An Men Nei Xin An Nan Li, Xuanwu District, Beijing ,100054, China
 *    All rights reserved.
 * 
 *    This software is the confidential and proprietary information of
 *    China Financial Certification Authority (&quot;Confidential Information&quot;).
 *    You shall not disclose such Confidential Information and shall use 
 *    it only in accordance with the terms of the license agreement you 
 *    entered into with CFCA.
 * </pre>
 */

package payment.simulator.institution.tx.aggregate;

import payment.api.system.AggregateEnvironment;
import payment.api.tx.aggregate.Tx5001Request;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Tx5001 extends HttpServlet {

    private static final long serialVersionUID = -7230771760624665867L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.取得参数
            String institutionID = request.getParameter("InstitutionID");
            String orderNo = request.getParameter("OrderNo");
            String merchantName = request.getParameter("MerchantName");
            String userID = request.getParameter("UserID");
            String amount = request.getParameter("Amount");
            String creditFlag = request.getParameter("CreditFlag");
            String paymentMode = request.getParameter("PaymentMode");
            String paymentWay = request.getParameter("PaymentWay");
            String settlementFlag = request.getParameter("SettlementFlag");
            String paymentScene = request.getParameter("PaymentScene");
            String goodsName = request.getParameter("GoodsName");
            String expirePeriod = request.getParameter("ExpirePeriod");
            String notificationURL = request.getParameter("NotificationURL");
            String remark = request.getParameter("Remark");

            // 2.创建交易请求对象
            Tx5001Request tx5001Request = new Tx5001Request();
            tx5001Request.setInstitutionID(institutionID);
            tx5001Request.setOrderNo(orderNo);
            tx5001Request.setMerchantName(merchantName);
            tx5001Request.setUserID(userID);
            tx5001Request.setAmount(amount);
            tx5001Request.setCreditFlag(creditFlag);
            tx5001Request.setPaymentMode(paymentMode);
            tx5001Request.setPaymentWay(paymentWay);
            tx5001Request.setSettlementFlag(settlementFlag);
            tx5001Request.setGoodsName(goodsName);
            tx5001Request.setPaymentScene(paymentScene);
            tx5001Request.setExpirePeriod(expirePeriod);
            tx5001Request.setNotificationURL(notificationURL);
            tx5001Request.setRemark(remark);

            // 3.执行报文处理
            tx5001Request.process();

            // 4.将参数放置到request对象
            // //3个交易参数
            request.setAttribute("plainText", tx5001Request.getRequestPlainText());
            request.setAttribute("message", tx5001Request.getRequestMessage());
            request.setAttribute("signature", tx5001Request.getRequestSignature());
            // //2个信息参数
            request.setAttribute("txCode", "5001");
            request.setAttribute("txName", "统一下单");
            // 1个action(支付平台地址)参数
            request.setAttribute("action", AggregateEnvironment.aggregatePaymentURL);

            // 5.转向Request.jsp页面
            request.getRequestDispatcher("/Request.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            processException(request, response, e.getMessage());
        }
    }

    private void processException(HttpServletRequest request, HttpServletResponse response, String errorMessage) {
        try {
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/ErrorPage.jsp").forward(request, response);
        } catch (Exception e) {
        }
    }

}
